package com.boot.chap07;

import io.netty.channel.Channel;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

/**
 * @author renwq
 * @since 2021/11/21 19:59
 */
public class ScheduleExamples {

    private void schedule() {
        ScheduledExecutorService schedule = Executors.newScheduledThreadPool(10);
        ScheduledFuture<?> future = schedule.schedule(() -> System.out.println("Now, it is 20 seconds later."), 20, TimeUnit.SECONDS);
        schedule.shutdown();
    }

    private void scheduleViaEventLoop() {
        Channel channel = new NioSocketChannel();
        ScheduledFuture<?> future = channel.eventLoop().schedule(
                new Runnable() {
                    @Override
                    public void run() {
                        System.out.println();
                    }
                },
                20, TimeUnit.SECONDS);
    }

    private void scheduleFixedViaEventLoop() {
        Channel channel = new NioSocketChannel();
        ScheduledFuture<?> future = channel.eventLoop().scheduleAtFixedRate(
                new Runnable() {
                    @Override
                    public void run() {
                        System.out.println();
                    }
                }, 20, 60, TimeUnit.SECONDS);
    }

    private void cancelTaskUsingScheduleFuture() {
        Channel channel = new NioSocketChannel();
        ScheduledFuture<?> future = channel.eventLoop().scheduleAtFixedRate(
                new Runnable() {
                    @Override
                    public void run() {
                        System.out.println();
                    }
                }, 20, 60, TimeUnit.SECONDS);
        future.cancel(true);
    }
}
